/******************************************************************************
* @file   main
* @brief  演示VirtLCD的使用方法
*         引用头文件:virtlcd.h
*         引用动态库:virtlcd.lib
* @author 蒋晓岗<kerndev@foxmail.com>
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stdint.h"
#include "windows.h"
#include "virtlcd.h"
#include "NesSystem.H"
#include "NesInput.h"

#pragma comment(lib,"virtLCD.lib")

#define LCD_X_SIZE 256 //水平分辨率
#define LCD_Y_SIZE 240 //垂直分辨率
#define LCD_BPP    16  //颜色位深: RGB565
#define LCD_SCALE  2   //放大2倍，像素点看得更清楚

uint16_t       *m_fb;
static BOOL    m_clicked;
static BOOL    m_exit;
static UINT16  m_color = 0xFFFF;

//画一个点
void lcd_draw_point(int x, int y, UINT16 color)
{
    m_fb[y * LCD_X_SIZE + x] = color;
}

//处理按键事件
void on_keybd_event(int event, int key)
{
    if(event == KEYBD_EVENT_DOWN)
    {
        printf("按键按下： %d\r\n", key);
        switch(key)
        {
            case '1': m_color = 0xFFFF; printf("白色\n");   break;
            case '2': m_color = 0xF800; printf("红色\n");   break;
            case '3': m_color = 0x07E0; printf("绿色\n");   break;
            case '4': m_color = 0x001F; printf("蓝色\n");   break;
            case '0': m_color = 0x0000; printf("黑色\n");   break;
            case 27: m_exit = TRUE; printf("程序退出\n");   break;

            case 'W': vNesInputBitSet(NES_JOYPAD1, NesInput_Up);          break;
            case 'w': vNesInputBitSet(NES_JOYPAD1, NesInput_Up);          break;
            case 'S': vNesInputBitSet(NES_JOYPAD1, NesInput_Down);        break;
            case 's': vNesInputBitSet(NES_JOYPAD1, NesInput_Down);        break;
            case 'A': vNesInputBitSet(NES_JOYPAD1, NesInput_Left);        break;
            case 'a': vNesInputBitSet(NES_JOYPAD1, NesInput_Left);        break;
            case 'D': vNesInputBitSet(NES_JOYPAD1, NesInput_Right);       break;
            case 'd': vNesInputBitSet(NES_JOYPAD1, NesInput_Right);       break;
			case 'J': vNesInputBitSet(NES_JOYPAD1, NesInput_B);           break;
			case 'j': vNesInputBitSet(NES_JOYPAD1, NesInput_B);           break;
			case 'K': vNesInputBitSet(NES_JOYPAD1, NesInput_A);           break;
			case 'k': vNesInputBitSet(NES_JOYPAD1, NesInput_A);           break;
            case 'V': vNesInputBitSet(NES_JOYPAD1, NesInput_Select);      break;
            case 'v': vNesInputBitSet(NES_JOYPAD1, NesInput_Select);      break;
            case 'B': vNesInputBitSet(NES_JOYPAD1, NesInput_Start);       break;
            case 'b': vNesInputBitSet(NES_JOYPAD1, NesInput_Start);       break;
        }
    }
    if(event == KEYBD_EVENT_UP)
    {
        printf("按键弹起： %d\r\n", key);
        switch(key)
        {
            case 'W': vNesInputBitReset(NES_JOYPAD1, NesInput_Up);        break;
            case 'w': vNesInputBitReset(NES_JOYPAD1, NesInput_Up);        break;
            case 'S': vNesInputBitReset(NES_JOYPAD1, NesInput_Down);      break;
            case 's': vNesInputBitReset(NES_JOYPAD1, NesInput_Down);      break;
            case 'A': vNesInputBitReset(NES_JOYPAD1, NesInput_Left);      break;
            case 'a': vNesInputBitReset(NES_JOYPAD1, NesInput_Left);      break;
            case 'D': vNesInputBitReset(NES_JOYPAD1, NesInput_Right);     break;
            case 'd': vNesInputBitReset(NES_JOYPAD1, NesInput_Right);     break;
            case 'K': vNesInputBitReset(NES_JOYPAD1, NesInput_A);         break;
            case 'k': vNesInputBitReset(NES_JOYPAD1, NesInput_A);         break;
            case 'J': vNesInputBitReset(NES_JOYPAD1, NesInput_B);         break;
            case 'j': vNesInputBitReset(NES_JOYPAD1, NesInput_B);         break;
            case 'V': vNesInputBitReset(NES_JOYPAD1, NesInput_Select);    break;
            case 'v': vNesInputBitReset(NES_JOYPAD1, NesInput_Select);    break;
            case 'B': vNesInputBitReset(NES_JOYPAD1, NesInput_Start);     break;
            case 'b': vNesInputBitReset(NES_JOYPAD1, NesInput_Start);     break;
        }
    }
}

//处理鼠标事件
void on_mouse_event(int event, int x, int y)
{
    if(event == MOUSE_EVENT_LBDOWN)
    {
        printf("鼠标按下: X=%d, Y=%d\n", x, y);
        m_clicked = 1;
    }
    if(event == MOUSE_EVENT_LBUP)
    {
        printf("鼠标弹起: X=%d, Y=%d\n", x, y);
        m_clicked = 0;
    }
    if(event == MOUSE_EVENT_MOVE)
    {
        printf("鼠标移动: X=%d, Y=%d\n", x, y);
        if(m_clicked)
        {
            lcd_draw_point(x, y, m_color);
        }
    }
}

int main(void)
{
    int ret;

    ret = VirtLCD_Init(LCD_X_SIZE, LCD_Y_SIZE, LCD_BPP, LCD_SCALE);
    if(ret == 0)
    {
        printf("初始化VirtLCD失败!\n");
        system("pause");
        return ret;
    }

    VirtLCD_SetKeybdProc(on_keybd_event);
    VirtLCD_SetMouseProc(on_mouse_event);
    m_fb = (UINT16 *)VirtLCD_GetFrameBuffer();
    printf("获取帧缓存: FrameBuffer = 0x%p\n", m_fb);
    printf("初始化成功!\n");

    vNesInit();
    while(1)
    {
        vNesExec();
    }

    while(m_exit==0)
    {
        Sleep(100);
    }
    VirtLCD_Exit();

    return ret;
}
